<?php
if(!defined('CMS'))die;
global$Eleanor;
$Eleanor->mconfig = include($Eleanor->module['path'].'config.php');
Eleanor::$Template->queue[] = $Eleanor->mconfig['admintpl'];

$lng = array_keys($Eleanor::$langs);
// $dir = scandir($Eleanor::$rootf .'modules');
$pathtofolder = $Eleanor::$rootf .'modules/';
// $pathtofolderlang = $Eleanor::$rootf .'lang/';
$pathtoroot = $Eleanor::$rootf;

$g = array();
$g = CheckLanguage(ScanFolders($pathtofolder, null, $lng), $lng);
$g += CheckLanguage(ScanFolders($pathtoroot, null, $lng), $lng);
// $g = CheckLanguageLng(ScanFolders($pathtofolder, null, $lng), $lng);

$c = Eleanor::$Template->TranslatesList($g);
Start();
echo $c;

function ScanFolders($path, $folder, $search) {
	if ($folder && is_dir($path . $folder))
		$path = $path . $folder;
	if (is_dir($path)) {
		$result = array();
		$dir = scandir($path);
		foreach ($dir as $v) {
			if ($v == "." || $v == "..")
				continue;
			if (is_dir($path . $v)) {
				if ($r = ScanFolders($path, $v, $search))
					$result[$v] = $r;
			}
			if (is_array($search)) {
				foreach ($search as &$d) {
					if (strpos($v, $d) !== false)
						$result[$d][] = $v;
				}
				unset($d);
			} elseif (ctype_alpha($search)) {
				if (strpos($v, $search) !== false)
					$result[$search][] = $v;
			}
		}
		return $result;
	}
}

function CheckLanguage($a, $lng) {
	$lng = array_combine($lng, $lng);
	foreach ($a as $k => $v) {
		// $first = reset($v);

		#Compare

		$name_file = array();
		foreach ($v as $l => $d) {
			foreach ($d as $g) {
				$ext = pathinfo($g, PATHINFO_EXTENSION);
				$name_file[$l][] = str_replace('-'. $l, null, pathinfo($g, PATHINFO_FILENAME));
				// $name_file[$l][] = $g;
			}
		}
		$first = reset($name_file);
		$first_key = key($name_file);
		$i = -1;
		foreach ($name_file as $l2 => $b) {
			$i++;
			if (!$i)
				continue;

			$add_to_first = array_diff($b, $first);
			if ($add_to_first) {
				$add_to_first = AddLng($add_to_first, $first_key, $ext);
				$a[$k][$first_key] += array('no' => $add_to_first);
			}
			$add_file = array_diff($first, $b);
			if ($add_file) {
				$add_file = AddLng($add_file, $l2, $ext);
				$a[$k][$l2] += array('no' => $add_file);
			}
		}
		#Compare end

		$add_lng = array_diff_key($lng, $v);
		if ($add_lng) {
			// dump(ReplaceLng($a[$k][$first_key], $first_key, key($add_lng)));//TODO
			$a[$k][key($add_lng)] = array('no' => ReplaceLng($a[$k][$first_key], $first_key, key($add_lng)));
		}
	}
	return $a;
}

####Service
/*
 * Clean multidimensional array
*/
function ClearArray($array) {
	foreach ($array as $k => &$v) {
		if (is_array($v)){
			$array[$k] = ClearArray($v);
			if(!count($array[$k]))
				unset($array[$k]);
		} else {
			// $v = trim($v);
			if (!trim($v))
				unset($array[$k]);
		}
	}
	unset($v);
	return $array;
}

function AddLng($a, $lng, $ext) {
	foreach($a as &$v) {
		if ($v) {
			$v = $v .'-'. $lng .'.'. $ext;
		} else {
			$v = $lng .'.'. $ext;
		}
	}
	return $a;
}

function ReplaceLng($a, $from_lng, $to_lng) {
	foreach($a as $k => $v) {
		if ($k === 'no') {
			$tmp = ReplaceLng($v, $from_lng, $to_lng);
			foreach ($tmp as &$d) {
				$a[] = $d;
			}
			unset($a[$k]);
		} else {
			$a[$k] = str_replace($from_lng, $to_lng, $v);
		}
	}
	return $a;
}
